package com.example.myapplication6_server;

import android.os.Handler;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;
public class MyServer implements Runnable {
    //定义保存所有Socket的ArrayList
    public static ArrayList<Socket> socketList=null;
    Handler handler;
    public MyServer(Handler handler){
        this.handler=handler;
        socketList=new ArrayList<>();
    }

    @Override
    public void run(){
        try{
            ServerSocket ss=new ServerSocket(30000);
            while(true){
                //此行代码会阻塞，将一直等待别人的连接
                Socket s = ss.accept();             ;
                socketList.add(s);             ;
                //每当客户端连接后启动一条ServerThread线程为该客户端服务
                new Thread(new ServerThread(s)).start();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
